package com.beeantec.jnrerc.modules.paper.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.core.api.dto.BaseIdReqDTO;
import com.beeantec.jnrerc.core.api.dto.BaseIdRespDTO;
import com.beeantec.jnrerc.core.api.dto.BaseIdsReqDTO;
import com.beeantec.jnrerc.core.api.dto.PagingReqDTO;
import com.beeantec.jnrerc.core.utils.BeanMapper;
import com.beeantec.jnrerc.dto.MemberVo;
import com.beeantec.jnrerc.dto.PaperDTO;
import com.beeantec.jnrerc.dto.PaperQuDTO;
import com.beeantec.jnrerc.dto.response.*;
import com.beeantec.jnrerc.entity.Paper;

import com.beeantec.jnrerc.dto.ext.PaperQuDetailDTO;
import com.beeantec.jnrerc.dto.request.PaperAnswerDTO;
import com.beeantec.jnrerc.dto.request.PaperCreateReqDTO;
import com.beeantec.jnrerc.dto.request.PaperListReqDTO;
import com.beeantec.jnrerc.dto.request.PaperQuQueryDTO;

import com.beeantec.jnrerc.entity.PaperQuAnswer;
import com.beeantec.jnrerc.entity.Qu;
import com.beeantec.jnrerc.entity.QuAnswerDTO;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import com.beeantec.jnrerc.modules.paper.service.PaperQuAnswerService;
import com.beeantec.jnrerc.modules.paper.service.PaperQuService;
import com.beeantec.jnrerc.modules.paper.service.PaperService;
import com.beeantec.jnrerc.core.utils.UserUtils;
import com.beeantec.jnrerc.modules.qu.service.QuAnswerService;
import com.beeantec.jnrerc.modules.qu.service.QuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* <p>
* 试卷控制器
* </p>
*
* @author Anthony
* @since 2020-05-25 16:33
*/
@Api(tags={"试卷"})
@RestController
@RequestMapping("/api/paper")
public class PaperController extends BaseController {

    @Autowired
    private PaperService baseService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private PaperQuAnswerService paperQuAnswerService;

    @Autowired
    private QuService quService;

    @Autowired
    private PaperQuService paperQuService;

    @Autowired
    private QuAnswerService quAnswerService;

    /**
    * 添加或修改
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiRest<BaseIdRespDTO> save(@RequestBody PaperDTO reqDTO) {
        //复制参数
        Paper entity = new Paper();
        BeanMapper.copy(reqDTO, entity);
        baseService.saveOrUpdate(entity);
        return super.success(new BaseIdRespDTO(entity.getId().toString()));
    }

    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
    * 查找详情
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public ApiRest<PaperDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        Paper entity = baseService.getById(reqDTO.getId());
        PaperDTO dto = new PaperDTO();
        BeanUtils.copyProperties(entity, dto);
        return super.success(dto);
    }



    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<PaperListRespDTO>> paging(@RequestBody PagingReqDTO<PaperListReqDTO> reqDTO) {

        //分页查询并转换
        IPage<PaperListRespDTO> page = baseService.paging(reqDTO);

        return super.success(page);
    }



    /**
     * 创建试卷
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "创建试卷")
    @RequestMapping(value = "/create-paper", method = { RequestMethod.POST})
    public ApiRest<BaseIdRespDTO> save(@RequestHeader("token")String token ,@RequestBody PaperCreateReqDTO reqDTO) {
        //复制参数
        MemberVo memberVo = memberService.token(token);
        String paperId = baseService.createPaper(memberVo.getMemberId().toString(), reqDTO.getExamId());
        return super.success(new BaseIdRespDTO(paperId));
    }

    /**
     * 试卷详情
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "试卷详情")
    @RequestMapping(value = "/paper-detail", method = { RequestMethod.POST})
    public ApiRest<ExamDetailRespDTO> paperDetail(@RequestBody BaseIdReqDTO reqDTO) {
        //根据ID删除
        ExamDetailRespDTO respDTO = baseService.paperDetail(reqDTO.getId());
        return super.success(respDTO);
    }

    /**
     * 问题详情
     * @return
     */
    @ApiOperation(value = "问题详情")
    @RequestMapping(value = "/qu-detail", method = { RequestMethod.GET})
    public ApiRest<PaperQuDetailDTO> quDetail(@RequestParam("paperId")String paperId,@RequestParam("quId")String quId) {
        //根据ID删除
        PaperQuDetailDTO respDTO = baseService.findQuDetail(paperId, quId);
        return super.success(respDTO);
    }

    /**
     * 填充答案
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "填充答案")
    @RequestMapping(value = "/fill-answer", method = { RequestMethod.POST})
    public ApiRest<PaperQuDetailDTO> fillAnswer(@RequestBody PaperAnswerDTO reqDTO) {
        //根据ID删除
        baseService.fillAnswer(reqDTO);
        return super.success();
    }


    /**
     * 交卷操作
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "交卷操作")
    @RequestMapping(value = "/hand-exam", method = { RequestMethod.POST})
    public ApiRest<PaperQuDetailDTO> handleExam(@RequestHeader("token")String token,@RequestBody BaseIdReqDTO reqDTO) {
        //根据ID删除
        MemberVo memberVo = memberService.token(token);
        baseService.handExam(reqDTO.getId(),memberVo.getMemberId().toString());
        return super.success();
    }


    /**
     * 批量删除
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "试卷详情")
    @RequestMapping(value = "/paper-result", method = { RequestMethod.POST})
    public ApiRest<ExamResultRespDTO> paperResult(@RequestBody BaseIdReqDTO reqDTO) {
        ExamResultRespDTO respDTO = baseService.paperResult(reqDTO.getId());
        return super.success(respDTO);
    }


    /**
     * 提交阅卷
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "提交阅卷")
    @RequestMapping(value = "/review-paper", method = { RequestMethod.POST})
    public ApiRest<PaperQuDetailDTO> reviewPaper(@RequestBody ExamResultRespDTO reqDTO) {
        //根据ID删除
        baseService.reviewPaper(reqDTO);
        return super.success();
    }


    /**
     * 获取考试下用户答题列表
     * @return
     */
    @ApiOperation(value = "获取考试下用户答题列表")
    @RequestMapping(value = "/memberAnswerList", method = { RequestMethod.GET})
    public ApiRest<List<MemberPaperQuDTO>> memberAnswerList(@RequestHeader("token") String token
            ,@RequestParam("examId")String examId,@RequestParam("paperId") String paperId) {
        List<MemberPaperQuDTO> memberPaperQuDTOS = new ArrayList<>();
        List<Qu> quList = new ArrayList<>();
        List<PaperQuDTO> paperQuDTOList = new ArrayList<>();

        paperQuDTOList = paperQuService.listByPaper(paperId);
        List<String> quIds = new ArrayList<>();
        for (PaperQuDTO paperQuDTO:paperQuDTOList){
            quIds.add(paperQuDTO.getQuId());
        }

        QueryWrapper<Qu> quWrapper = new QueryWrapper<>();
        quWrapper.lambda().in(Qu::getId,quIds);
        quList = quService.list(quWrapper);
       for (Qu qu:quList){
           List<QuAnswerDTO> quAnswerDTOS = new ArrayList<>();
           MemberPaperQuDTO memberPaperQuDTO = new MemberPaperQuDTO();
           BeanUtils.copyProperties(qu,memberPaperQuDTO);
           quAnswerDTOS =  quAnswerService.listByQu(qu.getId());
           memberPaperQuDTO.setList(getAnswers(quAnswerDTOS,paperId));
           memberPaperQuDTOS.add(memberPaperQuDTO);
       }
        return super.success(memberPaperQuDTOS);
    }


    private List<MemberPaperAnswerDTO> getAnswers(List<QuAnswerDTO> list,String paperId){
        List<MemberPaperAnswerDTO> memberPaperAnswerDTOS = new ArrayList<>();
        memberPaperAnswerDTOS = BeanMapper.mapList(list, MemberPaperAnswerDTO.class);
        for (MemberPaperAnswerDTO memberPaperAnswerDTO:memberPaperAnswerDTOS){
            QueryWrapper<PaperQuAnswer> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(PaperQuAnswer::getQuId,memberPaperAnswerDTO.getQuId())
                    .eq(PaperQuAnswer::getAnswerId,memberPaperAnswerDTO.getId())
            .eq(PaperQuAnswer::getPaperId,paperId);
           PaperQuAnswer paperQuAnswer = paperQuAnswerService.getOne(queryWrapper);
           if (null!=paperQuAnswer){
               memberPaperAnswerDTO.setChecked(paperQuAnswer.getChecked());
               memberPaperAnswerDTO.setIsRight(paperQuAnswer.getIsRight());
               memberPaperAnswerDTO.setSort(paperQuAnswer.getSort());
               memberPaperAnswerDTO.setAbc(paperQuAnswer.getAbc());
           }
        }
        return memberPaperAnswerDTOS;
    }
}
